package com.weipai.demo.service.impl;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.DpictureShowMapper;
import com.weipai.demo.mapper.base.PictureShowMapper;
import com.weipai.demo.model.PictureShow;
import com.weipai.demo.service.PictureService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/28 14:42
 * @Version 1.0
 */
@Service
public class PictureServiceImpl implements PictureService {

    private static final Logger log = LoggerFactory.getLogger(PictureServiceImpl.class);

    @Autowired
    private PictureShowMapper pictureShowMapper;

    @Autowired
    private DpictureShowMapper dpictureShowMapper;


    //保存图片
    @Override
    public Response savePicture(PictureShow pictureShow) {
        Response response = new Response();
        if (pictureShow.getId() == null) {

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String nowDate = df.format(new Date());
            pictureShow.setCreateTime(nowDate);
            pictureShowMapper.insert(pictureShow);
            response.setCode(RetCode.SUCCESS);

        } else {
            // int i1 = pictureShowMapper.updateByPrimaryKey(pictureShow);
            int i = pictureShowMapper.updateByPrimaryKeySelective(pictureShow);
            if (i > 0) {
                response.setCode(RetCode.UPDATESUCCESS);
            } else {
                response.setCode(RetCode.UPDATEERROR);
                return response;
            }
        }
        return response;
    }

    //分页展示图片
    @Override
    public LayuiTablePagingResult pictureShow(Integer page, Integer size, Integer pictureClass) {
        LayuiTablePagingResult result = null;
//        try {
//            PageHelper.startPage(page,size);

//            PictureShow pictureShow=new PictureShow();
//            if(pictureClass != null){
//                pictureShow.setPictureClass(pictureClass);
//            }
        Integer start = (page - 1) * size;
        List<Map<String, Object>> banners = dpictureShowMapper.getAllPicture(start, size, pictureClass);
        Long bannersNum = 0L;
        if(pictureClass == null){
            bannersNum = dpictureShowMapper.getAllPictureNums(pictureClass).longValue();
        }

//            List<PictureShow> banners = pictureShowMapper.select(pictureShow);
        //List<JobNews> banners = jobNewsMapper.select(jobNews);

//            PageInfo<PictureShow> pageInfo=new PageInfo<>(banners);
        result = new LayuiTablePagingResult();
        result.setCode(RetCode.SUCCESS.getCode());
        result.setMsg(RetCode.SUCCESS.getMsg());
        result.setCount(bannersNum);
        result.setData(banners);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error(e.getMessage());
//        }
        return result;
    }

    @Override
    public Response delpicture(Integer id) {
        Response response = new Response();
        if (id != null) {
           Integer rest =  pictureShowMapper.deleteByPrimaryKey(id);
           if(rest > 0){
               response.setCode(RetCode.SUCCESS);
           }else{
               response.setCode(RetCode.REQUEST_ERROR);
           }
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }
}
